package com.itheima.algorithm.binarytree;

/**
 * @author: TylerZhong
 * @description: 二叉树遍历
 */
public class TreeTraversal {

    static class TreeNode {
        TreeNode left;
        int val;
        TreeNode right;

        public TreeNode(TreeNode left, int val, TreeNode right) {
            this.left = left;
            this.val = val;
            this.right = right;
        }
    }

    /**
     * 前序遍历
     * @param node - 节点
     */
    public void preOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.print(node.val + "\t");
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * 中序遍历
     * @param node - 节点
     */
    public void inOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.print(node.val + "\t");
        inOrder(node.right);
    }

    /**
     * 后序遍历
     * @param node - 节点
     */
    public void postOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.print(node.val + "\t");
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(
                new TreeNode(
                        new TreeNode(null, 4, null),
                        2,
                        null
                ),
                1,
                new TreeNode(
                        new TreeNode(null, 5, null),
                        3,
                        new TreeNode(null, 6, null)
                )
        );
        
        TreeTraversal treeTraversal = new TreeTraversal();
        treeTraversal.preOrder(treeNode);
        System.out.println();
        treeTraversal.inOrder(treeNode);
        System.out.println();
        treeTraversal.postOrder(treeNode);

    }
}
